package com.atguigu.lease.web.app.custom.converter;

import com.atguigu.lease.model.enums.ItemType;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

/**
 * ClassName: StringToItemTypeConverter
 * Package: com.atguigu.lease.web.admin.custom.converter
 * Description:
 * StringToItem转换器
 *
 * @Autor: xvyy
 * @Create: 2024/11/16 - 15:14
 * @Version: v1.0
 */
@Component
//todo 实现Converter接口 <String 转 ItemType>
public class StringToItemTypeConverter implements Converter<String, ItemType> {
    @Override
    public ItemType convert(String source) {
        //todo 遍历ItemType枚举，如果枚举的code与source相等，则返回该枚举
        // ItemType.values() : 获取所有枚举值
        ItemType[] values = ItemType.values();
        for (ItemType value : values) {
            if (value.getCode().equals(Integer.valueOf(source))) {
                return value;
            }
        }
        //todo 如果没有匹配到，则抛出参数异常异常
        throw new IllegalArgumentException("code非法");
    }
}
